package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.redis.StatisticsSummerActivityRedisDao;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 夏季活动统计
 * @author xuanqicheng
 */
@Service
public class SummerActivityStatisticService {


    private static final String TIME_KEY = "coolSummerActivities";

    @Autowired
    private StatisticsSummerActivityRedisDao statisticsSummerActivityRedisDao;

    @Autowired
    private AppKeyService appKeyService;

    /**
     * 统计夏季活动
     * @param type 1 寻寻页面的悬浮广告位，2：社区广告位，3：参与暑假活动
     */
    public void getStatisticsSummerActivity(int type,long userId) {
        Appkey appkey = appKeyService.getAppKeyByKey(TIME_KEY);

        LocalDate localDate = LocalDate.now();

        if (appkey != null) {

            JSONObject json = JSONObject.parseObject(appkey.getValue());

            String startDateTarget = json.getString("startDate");

            String endDateTarget = json.getString("endDate");

            LocalDate startDate = LocalDate.parse(startDateTarget,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            LocalDate endDate = LocalDate.parse(endDateTarget,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"));


            if (DateUtil.checkBetweenTime(startDate, endDate, localDate)) {
                if(type == 1){
                    statisticsSummerActivityRedisDao.summerActivityXunXunOnclickNumbers(userId);
                }else if(type == 2){
                    statisticsSummerActivityRedisDao.summerActivityDynamicOnclickNumbers(userId);
                }else if(type == 3){
                    statisticsSummerActivityRedisDao.summerActivityNumbersOfParticipantsKey(userId);
                }
            }

        }
    }


}
